﻿using iTFF.WPF.CP;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using iTFF.WPF.Model.Lyrics;
using System.Collections.Generic;
using System.Linq;
using System;
using iTFF.WPF.Model.Lyrics.LocalProviders;

namespace iTFF.Tests
{
    
    
    /// <summary>
    ///This is a test class for LyricsControlPanelTest and is intended
    ///to contain all LyricsControlPanelTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LyricsControlPanelTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            // link with itunes
            var cp = ControlPanel.Instance.iTunesControlPanel;
        }

        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            // dispose all
            ControlPanel.Instance.Dispose();
        }
        //
        #endregion


        /// <summary>
        ///A test for WebProviders
        ///</summary>
        [TestMethod()]
        public void WebProvidersTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            Assert.IsTrue(target.WebProviders.Length > 0, "No web provider found");
        }

        /// <summary>
        ///A test for LocalProviders
        ///</summary>
        [TestMethod()]
        public void LocalProvidersTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            Assert.IsTrue(target.LocalProviders.Length > 0, "No Local provider found");
        }

        /// <summary>
        ///A test for ActivatedLocalProviders
        ///</summary>
        [TestMethod()]
        public void ActivatedLocalProvidersTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            // activate file provider
            var fileProvider = target.GetLocalProvider(typeof(FileLocalProvider));
            fileProvider.IsActivated = true;

            Assert.IsFalse(target.ActivatedAndValidLocalProviders.Any());
            
            // validate it
            ((FileLocalProvider)fileProvider).FolderPath = TestContext.TestDir;

            // check if in collection
            Assert.AreSame(target.ActivatedAndValidLocalProviders.First(), fileProvider, 
                "ActivatedLocalProviders collection doesn't return the good value");
        }

        /// <summary>
        ///A test for SaveLyrics
        ///</summary>
        [TestMethod()]
        public void SaveLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            var id3Provider = target.GetLocalProvider(typeof(id3LocalProvider));
            var fileProvider = target.GetLocalProvider(typeof(FileLocalProvider));
            var sqlProvider = target.GetLocalProvider(typeof(SQLCeLocalProvider));

            // with no activated local provider
            Assert.IsFalse(target.SaveLyrics(lyricsSample, true));

            // with id3 provider
            id3Provider.IsActivated = true;
            Assert.IsTrue(target.SaveLyrics(lyricsSample, true));
            var res = target.GetLyrics(lyricsSample.iTunesPersistentID);
            Assert.AreEqual(lyricsSample, target.GetLyrics(lyricsSample.iTunesPersistentID));

            // with id3 provider and sql provider
            sqlProvider.IsActivated = true;

            Assert.IsTrue(target.SaveLyrics(lyricsSample, true));
            Assert.AreEqual(lyricsSample, target.GetLyrics(lyricsSample.iTunesPersistentID));

            /// check if exists in both
            Assert.AreEqual(lyricsSample, sqlProvider.GetLyrics(lyricsSample.iTunesPersistentID));
            Assert.AreEqual(lyricsSample, id3Provider.GetLyrics(lyricsSample.iTunesPersistentID));

        }

        /// <summary>
        ///A test for RemoveLyrics
        ///</summary>
        [TestMethod()]
        public void RemoveLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            var id3Provider = target.GetLocalProvider(typeof(id3LocalProvider));
            var fileProvider = target.GetLocalProvider(typeof(FileLocalProvider));
            var sqlProvider = target.GetLocalProvider(typeof(SQLCeLocalProvider));

            // with no activated local provider
            Assert.IsFalse(target.RemoveLyrics(lyricsSample.iTunesPersistentID));

            // activate 2 providers, save the lyrics, delete them
            id3Provider.IsActivated = true;
            sqlProvider.IsActivated = true;
            target.SaveLyrics(lyricsSample, true);
            target.RemoveLyrics(lyricsSample.iTunesPersistentID);

            Assert.IsFalse(id3Provider.LyricsExists(lyricsSample.iTunesPersistentID), "id3 lyrics not removed");
            Assert.IsFalse(sqlProvider.LyricsExists(lyricsSample.iTunesPersistentID), "SQL lyrics not removed");
            Assert.IsFalse(target.LyricsExists(lyricsSample.iTunesPersistentID), "Not all lyrics removed");

        }

        /// <summary>
        ///A test for LyricsExist
        ///</summary>
        [TestMethod()]
        public void LyricsExistTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            var id3Provider = target.GetLocalProvider(typeof(id3LocalProvider));
            var fileProvider = target.GetLocalProvider(typeof(FileLocalProvider));
            var sqlProvider = target.GetLocalProvider(typeof(SQLCeLocalProvider));

            // activate two providers, save the lyrics
            id3Provider.IsActivated = true;
            sqlProvider.IsActivated = true;

            target.SaveLyrics(lyricsSample, true);

            Assert.IsTrue(target.LyricsExists(lyricsSample.iTunesPersistentID));
            
            // delete lyrics from id3
            id3Provider.RemoveLyrics(lyricsSample.iTunesPersistentID);
            Assert.IsTrue(target.LyricsExists(lyricsSample.iTunesPersistentID));

            // delete the re-check
            target.RemoveLyrics(lyricsSample.iTunesPersistentID);
            Assert.IsFalse(target.LyricsExists(lyricsSample.iTunesPersistentID));

            // re -set in id3 to keep it
            id3Provider.SaveLyrics(lyricsSample, false);
        }

        /// <summary>
        ///A test for GetWebProvider
        ///</summary>
        [TestMethod()]
        public void GetWebProviderTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            Assert.IsNotNull(target.GetWebProvider("www.wiki-lyrics.org"));
        }

        /// <summary>
        ///A test for GetLyrics
        ///</summary>
        [TestMethod()]
        public void GetLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            var id3Provider = target.GetLocalProvider(typeof(id3LocalProvider));
            var fileProvider = target.GetLocalProvider(typeof(FileLocalProvider));
            var sqlProvider = target.GetLocalProvider(typeof(SQLCeLocalProvider));

            // activate two providers, save the lyrics
            id3Provider.IsActivated = true;
            sqlProvider.IsActivated = true;

            // save lyrics in sql only and remove from sql
            var v = sqlProvider.IsValid;
            if (sqlProvider.LyricsExists(lyricsSample.iTunesPersistentID))
                sqlProvider.RemoveLyrics(lyricsSample.iTunesPersistentID);
            id3Provider.SaveLyrics(lyricsSample, true);

            Assert.AreEqual(lyricsSample, target.GetLyrics(lyricsSample.iTunesPersistentID));
        }

        /// <summary>
        ///A test for FetchLyrics
        ///</summary>
        [TestMethod()]
        public void FetchLyricsWikiLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            LyricsModel actual;

            // check for wiki lyrics
            actual = target.FetchLyrics("Aaron", "U-Turn (Lili)", target.GetWebProvider("www.wiki-lyrics.org"));
            Assert.AreEqual(lyricsSample.Lyrics, actual.Lyrics);
            Assert.AreEqual(lyricsSample.Artist, actual.Artist);
            Assert.AreEqual(lyricsSample.Name, actual.Name);
        }

        /// <summary>
        ///A test for FetchLyrics
        ///</summary>
        [TestMethod()]
        public void FetchLyricsLeoLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();
            LyricsModel actual;

            // check for Leo's Lyrics
            actual = target.FetchLyrics("Aaron", "U-Turn (Lili)", target.GetWebProvider("Leo's Lyrics"));
            Assert.AreEqual(@"Lili, take another walk out of your fake world
please put all the drugs out of your hand
you'll see that you can breathe with no back up
so much stuff youve got to understand
for every step in any walk
any town of any thought
I'll be your guide
for every street of any scene
any place you've never been
I'll be your guide
Lili, you know there's still a place for people like us
the same blood runs in every hand
you see its not the wings that make the angel
just have to move the bat out of your head
for every step in any walk
any town of any thought
I'll be your guide
for every street of any scene
any place you've never been
I'll be your guide
Lili, easy as a kiss we'll find an answer
put all your fears back in the shade
don't become a ghost with no colour
cause you're the best paint, life ever made", actual.Lyrics, "Lyrics different");
            Assert.AreEqual(lyricsSample.Artist, actual.Artist, true);
            Assert.AreEqual("U-Turn - Lili", actual.Name, true);

        }

        /// <summary>
        ///A test for FetchLyrics
        ///</summary>
        [TestMethod()]
        public void FetchLyricsSeekLyricsTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            LyricsModel actual;

            actual = target.FetchLyrics("Red Hot Chili Peppers", "Californication", target.GetWebProvider("SeekLyrics"));

            // there is an encoding probleme with seek lyrics website : 
            // http://seeklyrics.com/lyrics/Red-Hot-Chili-Peppers/Californication.html
            Assert.AreEqual(@"Psychic spies from China
Try to steal your mind’s elation
Little girls from Sweden
Dream of silver screen quotations
And if you want these kind of dreams
It’s Californication

It’s the edge of the world
And all of western civilization
The sun may rise in the East
At least it settles in the final location
It’s understood that Hollywood
sells Californication

Pay your surgeon very well
To break the spell of aging
Celebrity skin is this your chin
Or is that war your waging

Chorus:
First born unicorn
Hard core soft porn
Dream of Californication
Dream of Californication

Marry me girl be my fairy to the world
Be my very own constellation
A teenage bride with a baby inside
Getting high on information
And buy me a star on the boulevard
It’s Californication

Space may be the final frontier
But it’s made in a Hollywood basement
Cobain can you hear the spheres
Singing songs off station to station
And Alderon’s not far away
It’s Californication

Born and raised by those who praise
Control of population everybody’s been there
and
I don’t mean on vacation

Chorus

Destruction leads to a very rough road
But it also breeds creation
And earthquakes are to a girl’s guitar
They’re just another good vibration
And tidal waves couldn’t save the world
From Californication

Pay your surgeon very well
To break the spell of aging
Sicker than the rest
There is no test
But this is what you’re craving

Chorus", actual.Lyrics);
            Assert.AreEqual("Red Hot Chili Peppers", actual.Artist);
            Assert.AreEqual("Californication", actual.Name);
        }

        /// <summary>
        ///A test for FetchLyrics
        ///</summary>
        [TestMethod()]
        public void FetchLyricsTest1()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            var lyricsSample = TrackConstants.GetSampleLyricsModel();

            var actual = target.FetchLyrics("Aaron", "U-Turn (Lili)", target.GetWebProvider("www.wiki-lyrics.org"));
            Assert.AreEqual(lyricsSample.Lyrics, actual.Lyrics);
            Assert.AreEqual(lyricsSample.Artist, actual.Artist);
            Assert.AreEqual(lyricsSample.Name, actual.Name);
        }

        /// <summary>
        ///A test for GetLocalProvider
        ///</summary>
        [TestMethod()]
        public void GetLocalProviderTest()
        {
            LyricsControlPanel target = new LyricsControlPanel();

            Assert.IsNotNull(target.GetLocalProvider(typeof(id3LocalProvider)));
        }
    }
}
